﻿<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">

    <!-- ContentDetail: Boolean-, Integer-, Link-, Double-, DateTime-, String-, Object-Detail -->
    <class name="FlexiCommerce.Details.ContentDetail,FlexiCommerce" table="Flexi_ItemDetail">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0">
            <generator class="native"/>
        </id>
        <discriminator column="Type" type="String" />

        <many-to-one name="EnclosingItem" column="ItemID" class="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce" not-null="true" fetch="select" />
        <many-to-one name="EnclosingCollection" column="DetailCollectionID" class="FlexiCommerce.Details.DetailCollection,FlexiCommerce" fetch="select" lazy="proxy"/>

        <property name="Name" type="String" length="50" />

        <subclass name="FlexiCommerce.Details.BooleanDetail,FlexiCommerce" discriminator-value="Bool">
            <property name="BoolValue" type="Boolean" />
        </subclass>

        <subclass name="FlexiCommerce.Details.IntegerDetail,FlexiCommerce" discriminator-value="Int">
            <property name="IntValue" type="Int32" />
        </subclass>

        <subclass name="FlexiCommerce.Details.LinkDetail,FlexiCommerce" discriminator-value="Link">
            <many-to-one name="LinkedItem" column="LinkValue" class="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce" lazy="proxy" />
        </subclass>

        <subclass name="FlexiCommerce.Details.DoubleDetail,FlexiCommerce" discriminator-value="Double">
            <property name="DoubleValue" type="Double" />
        </subclass>

        <subclass name="FlexiCommerce.Details.DateTimeDetail,FlexiCommerce" discriminator-value="DateTime">
            <property name="DateTimeValue" type="DateTime" />
        </subclass>

        <subclass name="FlexiCommerce.Details.StringDetail,FlexiCommerce" discriminator-value="String">
            <property name="StringValue" type="StringClob" length="1073741823" />
        </subclass>

        <subclass name="FlexiCommerce.Details.ObjectDetail,FlexiCommerce" discriminator-value="Object">
            <property name="Value" type="Serializable" length="2147483647" />
        </subclass>
    </class>

    <!-- GenericPersistenceItem -->
    <class name="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce" table="Flexi_Item">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>

        <discriminator column="Type" type="String" />

        <property name="Created"	  access="property" not-null="true" />
        <property name="Updated"    access="property" not-null="true" />
        <property name="Expires"	  access="property" />

        <property name="Name"       access="property" length="255" />
        <property name="ZoneName"	  access="property" length="50" />
        <property name="Title"		  access="property" length="255" />
        <property name="SortOrder"	access="property" not-null="true" />
        <property name="Visible"	  access="property" not-null="true" />
        <property name="SavedBy"	  access="property" length="50" />


        <many-to-one name="Parent"		column="ParentID"		class="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce" not-null="false" lazy="proxy" fetch="select" />

        <bag name="Children" cascade="all" generic="true" inverse="true" order-by="SortOrder" lazy="true">
            <cache usage="read-write" />
            <key column="ParentID" />
            <one-to-many class="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce"/>
        </bag>

        <map name="Details" inverse="true" cascade="all-delete-orphan" generic="true" where="DetailCollectionID IS NULL" lazy="true">
            <cache usage="read-write" />
            <key column="ItemID" />
            <index column="Name" type="String" />
            <one-to-many class="FlexiCommerce.Details.ContentDetail, FlexiCommerce"/>
        </map>

        <map name="DetailCollections" inverse="true" cascade="all-delete-orphan" generic="true" lazy="true">
            <cache usage="read-write" />
            <key column="ItemID" />
            <index column="Name" type="String" />
            <one-to-many class="FlexiCommerce.Details.DetailCollection, FlexiCommerce"/>
        </map>


    </class>
    
    <!-- DetailCollection -->
    <class name="FlexiCommerce.Details.DetailCollection,FlexiCommerce" table="Flexi_ItemDetailCollection">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0">
            <generator class="native"/>
        </id>

        <many-to-one name="EnclosingItem" column="ItemID" class="FlexiCommerce.Components.GenericPersistenceItem,FlexiCommerce" />
        <property name="Name"			type="String" not-null="true" length="50" />

        <bag name="Details" generic="true" lazy="false" cascade="all-delete-orphan" inverse="true">
            <cache usage="nonstrict-read-write"/>
            <key column="DetailCollectionID" />
            <one-to-many class="FlexiCommerce.Details.ContentDetail,FlexiCommerce"/>
        </bag>
    </class>
    
    <!-- Language -->
    <class name="FlexiCommerce.Components.Language,FlexiCommerce" table="Flexi_Languages">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>

        <property name="Culture" type="FlexiCommerce.Components.Culture, FlexiCommerce" not-null="true" length="256" />

    </class>
    
    <!-- Category -->
    <class name="FlexiCommerce.Components.Category,FlexiCommerce" table="Flexi_Category">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>
        
        <property name="SortOrder"	access="property" not-null="true" />
        <property name="Name"       access="property" length="20" />
        <property name="Description"       access="property" length="100" />
        
        <many-to-one name="Parent"		column="ParentID"		class="FlexiCommerce.Components.Category,FlexiCommerce" not-null="false" lazy="proxy" fetch="select" />

        <bag name="Children" cascade="all" generic="true" inverse="true" order-by="SortOrder" lazy="true">
            <cache usage="read-write" />
            <key column="ParentID" />
            <one-to-many class="FlexiCommerce.Components.Category,FlexiCommerce"/>
        </bag>

        <bag name="Details" cascade="all" generic="true" inverse="true">
            <cache usage="read-write" />
            <key column="CategoryID"/>
            <one-to-many
               class="FlexiCommerce.Components.CategoryDetails,FlexiCommerce"/>
        </bag>
        
        <bag name="Products" lazy="true" generic="true" table="Flexi_ProductCategoryMapping" cascade="none">
            <cache usage="read-write" />
            <key column="ID2" />
            <many-to-many class="FlexiCommerce.Components.Product,FlexiCommerce" column="ID" />
        </bag>

    </class>

    <!-- Category Details -->
    <class name="FlexiCommerce.Components.CategoryDetails,FlexiCommerce" table="Flexi_CategoryDetails">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>

        <many-to-one name="Category" column="CategoryID" class="FlexiCommerce.Components.Category, FlexiCommerce"  fetch="select" />
        <property name="Name" access="property" length="20" />
        <property name="Description" access="property" length="100" />
        <property name="Culture" type="FlexiCommerce.Components.Culture, FlexiCommerce" not-null="true" length="256" />

    </class>

    <!-- Product -->
    <class name="FlexiCommerce.Components.Product,FlexiCommerce" table="Flexi_Product">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>
        <property name="Name"       access="property" length="20" />

        <bag name="Details" cascade="all" generic="true" inverse="true">
            <cache usage="read-write" />
            <key column="ProductID"/>
            <one-to-many
               class="FlexiCommerce.Components.ProductDetails,FlexiCommerce"/>
        </bag>

        <bag name="Categories" generic="true" lazy="true" table="Flexi_ProductCategoryMapping" cascade="none">
            <cache usage="read-write" />
            <key column="ID" />
            <many-to-many class="FlexiCommerce.Components.Category,FlexiCommerce" column="ID2" />
        </bag>

        <bag name="Images" generic="true" lazy="true" table="Flexi_ProductImageMapping" cascade="none">
            <cache usage="read-write" />
            <key column="ProductID"></key>
            <many-to-many class="FlexiCommerce.Components.Image,FlexiCommerce" column="ImageID"></many-to-many>
        </bag>
        
    </class>
    
    <!-- Product Details -->
    <class name="FlexiCommerce.Components.ProductDetails,FlexiCommerce" table="Flexi_ProductDetails">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>

        <many-to-one name="Product" column="ProductID" class="FlexiCommerce.Components.Product, FlexiCommerce"  fetch="select" />
        <property name="Name" access="property" length="20" />
        <property name="Culture" type="FlexiCommerce.Components.Culture, FlexiCommerce" not-null="true" length="256" />

    </class>
    
    <!-- Image -->
    <class name="FlexiCommerce.Components.Image,FlexiCommerce" table="Flexi_Images">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>
        <property name="Bitmap" type="Serializable" length="2147483647" />
        <property name="Hash" access="property"/>

    </class>

    <!-- Configuration -->
    <class name="FlexiCommerce.Components.Configuration,FlexiCommerce" table="Flexi_Configuration">
        <cache usage="read-write" />

        <id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
            <generator class="native" />
        </id>
        <property name="StoreXml" length="2147483647" update="true" insert="true"   />

    </class>


</hibernate-mapping>
